using System;
using System.Linq;
using ECommerce.IntegrationSpecs;
using ECommerce.IntegrationSpecs.TestDataBuilders;
using ECommerce.Web.Commands;
using ECommerce.Web.Models;
using ECommerce.Web.Services.ShoppingSessions;
using Machine.Specifications;
using NHibernate;
using Rhino.Mocks;

namespace Add_product_to_basket_command_specs
{
    [Subject("Adding a product to the basket")]
    public class When_adding_a_product_to_the_basket : InMemoryDatabaseContext
    {
        private static Product product;
        private static Guid shopperToken;
        private static IShoppingSession shoppingSession;
        private static AddProductToBasketMessage addProductToBasketMessage;
        private static IAddProductToBasketCommand addProductToBasketCommand;

        Establish context = () =>
        {
            using (ITransaction transaction = Session.BeginTransaction())
            {
                product = new ProductBuilder().Build();

                Session.Save(product); 

                transaction.Commit();
            }

            shopperToken = Guid.NewGuid();

            shoppingSession = MockRepository.GenerateStub<IShoppingSession>();
            shoppingSession.Stub(x => x.GetShopperToken()).Return(shopperToken);

            addProductToBasketMessage = new AddProductToBasketMessage()
                                            {
                                                ProductId = product.Id,
                                                Quantity = 1
                                            };

            addProductToBasketCommand = new AddProductToBasketCommand(shoppingSession);
        };
        
        Because of = () => addProductToBasketCommand.Handle(addProductToBasketMessage);

        It should_add_product_to_basket = () =>
        {
            Session.Clear();
            
            var shopper = Session.Get<Shopper>(shopperToken);

            shopper.Basket.Items.Any(x => x.Product.Id == product.Id).ShouldBeTrue();
        };
    }
}